﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Diagnostics;
using SiteManager;


public partial class includes_uAddVideo : System.Web.UI.UserControl
{

    int DoctorId;
    protected void Page_Load(object sender, EventArgs e)
    {
        //DoctorId = Convert.ToInt32(Session["userID"]);
        // DoctorId = Convert.ToInt32(Session["DrId"].ToString());
        btnSubmit.Attributes.Add("onclick", "return checkTitle('" + txtTitle.ClientID + "','" + txtDescription.ClientID + "');");
    }


    protected void btnSubmit_Click(object sender, EventArgs e)
    {

        DoctorId = Convert.ToInt32(Session["userID"]);
        //if (VideoUpload.HasFile && VideoUpload.PostedFile != null && VideoUpload.PostedFile.FileName != "")
        //{

        //    // AddVideo(DoctorId);

        //    ReturnVideo(this.VideoUpload.FileName.ToString(), DoctorId);
        //    // VidoLod();
        //}

        if (!string.IsNullOrEmpty(txtTag.Text))
        {
            AddFbVideo(DoctorId);
        }

    }

    private void AddFbVideo(int drId)
    {
        tbl_DoctorFBVideo docProfile = new tbl_DoctorFBVideo();
        docProfile.Dr_Id = drId;
        // docProfile.Video_Name = filename1;
        docProfile.Title = txtTitle.Text; ;
        docProfile.Description = txtDescription.Text;
        docProfile.FbUrl = txtTag.Text; //CommonFunction.GetYoutubeVideoID(txtTag.Text);

        ProfileRipository.AddDoctorFBVideo(docProfile);
    }
    //private void AddVideo(int drId)
    //{
    //    if (Request.QueryString["Id"] != null)
    //    {
    //        //DoctorId = Convert.ToInt32(Request.QueryString["Id"].ToString());
    //    }
    //    else if (RewriteModule.RewriteContext.Current.Params["Id"] != null)
    //    {
    //        // DoctorId = Convert.ToInt32(RewriteModule.RewriteContext.Current.Params["Id"].ToString());
    //    }
    //    byte[] buffer;
    //    if (VideoUpload.HasFile && VideoUpload.PostedFile != null && VideoUpload.PostedFile.FileName != "")
    //    {

    //        HttpPostedFile file = VideoUpload.PostedFile;
    //        buffer = new byte[file.ContentLength];
    //        Manager ObjManager = new Manager(true, "ConnectionString");

    //        if (VideoUpload.HasFile)
    //        {
    //            string ImgPath1 = "../images/Dr_images/";
    //            string filename1 = Path.GetFileName(VideoUpload.PostedFile.FileName);
    //            string baseFile = Path.GetFileNameWithoutExtension(VideoUpload.PostedFile.FileName);
    //            string extension = Path.GetExtension(VideoUpload.PostedFile.FileName);
    //            int fileMatchCount = 1;
    //            // Check if a file with the given name exists in the Images1 subfolder of the root folder of your site
    //            while (File.Exists(Server.MapPath(Path.Combine(ImgPath1, filename1))))
    //            {
    //                //filename1 = string.Format("{0}({1:D3}){2}", baseFile, fileMatchCount, extension);
    //                filename1 = string.Format("{0}({1:D2}){2}", baseFile, fileMatchCount + ".flv");
    //                fileMatchCount++;
    //            }
    //            VideoUpload.SaveAs(Server.MapPath(Path.Combine(ImgPath1, filename1)));
    //            //Session["photo"] = filename1;
    //            if (filename1.Length > 0)
    //            {
    //                ViewState["sfilename"] = filename1;
    //            }
    //        }

    //        int bytesReaded = file.InputStream.Read(buffer, 0, VideoUpload.PostedFile.ContentLength);
    //        if (bytesReaded > 0)
    //        {
    //            tbl_DoctorVideo docProfile = new tbl_DoctorVideo();
    //            docProfile.dr_Id = drId;
    //            docProfile.Video_Name = file.FileName;
    //            docProfile.Title = txtTitle.Text; ;
    //            docProfile.Description = txtDescription.Text;
    //            docProfile.Video = buffer;
    //            docProfile.Video_Size = file.ContentLength;
    //            ProfileRipository.AddDoctorVideo(docProfile);
    //        }



    //    }
    //}

    //private bool ReturnVideo(string fileName, int drId)
    //{
    //    string filename1 = "";
    //    string imageName = "";
    //    string html = string.Empty;

    //    //rename if file already exists

    //    int j = 0;

    //    string AppPath;

    //    string inputPath;

    //    string outputPath;

    //    string imgpath;

    //    AppPath = Request.PhysicalApplicationPath;

    //    //Get the application path
    //    inputPath = Server.MapPath("~/OriginalVideo");
    //   ///// inputPath = AppPath + "OriginalVideo";

    //    //Path of the original file

    //    outputPath = Server.MapPath("~/ConvertVideo");// AppPath + "ConvertVideo";

    //    //Path of the converted file

    //    imgpath = Server.MapPath("~/Thumbs");//AppPath + "Thumbs";

    //    //Path of the preview file

    //    string filepath = Server.MapPath("~/OriginalVideo/" + fileName);
    //    string fileWithoutExt = Path.GetFileNameWithoutExtension(fileName);
    //    filename1 = string.Format(fileWithoutExt + ".flv");
    //    string videoPath = Server.MapPath("~/ConvertVideo/" + filename1);//"/ConvertVideo/" + filename1;
    //    imageName = string.Format(fileWithoutExt + ".jpg");
    //    while (File.Exists(filepath))
    //    {

    //        j = j + 1;

    //        int dotPos = fileName.LastIndexOf(".");

    //        string namewithoutext = fileName.Substring(0, dotPos);

    //        string ext = fileName.Substring(dotPos + 1);

    //        fileName = namewithoutext + j + "." + ext;

    //        filepath = Server.MapPath("~/OriginalVideo/" + fileName);
    //        string baseFile = Path.GetFileNameWithoutExtension(fileName);
    //        filename1 = string.Format(baseFile + ".flv");
    //        imageName = string.Format(baseFile + ".jpg");
    //        videoPath = Server.MapPath("~/ConvertVideo/" + filename1); //"/ConvertVideo/" + filename1;


    //    }

    //    try
    //    {


    //        this.VideoUpload.SaveAs(filepath);
    //        tbl_DoctorVideo docProfile = new tbl_DoctorVideo();
    //        docProfile.dr_Id = drId;
    //        docProfile.Video_Name = filename1;
    //        docProfile.Title = txtTitle.Text; ;
    //        docProfile.Description = txtDescription.Text;
    //        docProfile.Image = imageName;
    //        // docProfile.Video = buffer;
    //        // docProfile.Video_Size = file.ContentLength;
    //        ProfileRipository.AddDoctorVideo(docProfile);

    //    }

    //    catch
    //    {

    //        return false;

    //    }

    //    string outPutFile;

    //    outPutFile =  "~/OriginalVideo/" + fileName;

    //    int i = this.VideoUpload.PostedFile.ContentLength;

    //    System.IO.FileInfo a = new System.IO.FileInfo(Server.MapPath(outPutFile));

    //    while (a.Exists == false)
    //    {

    //    }

    //    long b = a.Length;

    //    while (i != b)
    //    {

    //    }

    //    string cmd = " -i \"" + inputPath + "\\" + fileName + "\" \"" + outputPath + "\\" + fileName.Remove(fileName.IndexOf(".")) + ".flv" + "\"";
    //    // changeVideoType(filepath, cmd);
    //    ConvertNow(cmd);

    //    string imgargs = " -i \"" + outputPath + "\\" + fileName.Remove(fileName.IndexOf(".")) + ".flv" + "\" -f image2 -ss 1 -vframes 1 -s 280x200 -an \"" + imgpath + "\\" + fileName.Remove(fileName.IndexOf(".")) + ".jpg" + "\"";
    //    //string filename = Path.GetFileName(imgargs);
    //    ConvertNow(imgargs);
    //    return true;
    //}


    //private void ConvertNow(string cmd)
    //{

    //    string exepath;

    //    string AppPath = Request.PhysicalApplicationPath;

    //    //Get the application path

    //    exepath = Server.MapPath("~/ffmpeg.exe");//AppPath + "ffmpeg.exe";

    //    System.Diagnostics.Process proc = new System.Diagnostics.Process();

    //    proc.StartInfo.FileName = exepath;

    //    //Path of exe that will be executed, only for "filebuffer" it will be "flvtool2.exe"

    //    proc.StartInfo.Arguments = cmd;

    //    //The command which will be executed

    //    proc.StartInfo.UseShellExecute = false;

    //    proc.StartInfo.CreateNoWindow = true;

    //    proc.StartInfo.RedirectStandardOutput = false;

    //    proc.Start();

    //    while (proc.HasExited == false)
    //    {

    //    }


    //}
    //public void VidoLod()
    //{
    //    string upFileName = VideoUpload.FileName;
    //    //judge whether the file has been selected
    //    if (this.VideoUpload.HasFile)
    //    {
    //        //obtain the file extension
    //        string upExtension = upFileName.Substring(upFileName.LastIndexOf(".") + 1);
    //        //judge whether the extension is valid
    //        //if (checkExtension(upExtension))
    //        //{
    //        //obtain the upload file save path
    //        string upFilePath = Server.MapPath("../OriginalVideo/") + upFileName;
    //        //store the file to the specified path
    //        VideoUpload.SaveAs(upFilePath);
    //        //obtain the current datetime
    //        string saveName = DateTime.Now.ToString("yyyyMMddHHmmssffff");
    //        //get the transformed file related path and name
    //        string playFile = "ConvertVideo/" + saveName + ".flv";
    //        // obtain the image related path and name
    //        string imgFile = "imgFile/" + saveName + ".jpg";
    //        try
    //        {
    //            // Determine whether the uploaded file is the .flv format
    //            if (upExtension == "flv")
    //            {
    //                //if the format is .flv then we directly save it to the specified path
    //                File.Copy(VideoUpload.PostedFile.FileName, Server.MapPath("../" + playFile));
    //                // invoke the helper catchImg to capture video image
    //                //  operateMethod.catchImg(Server.MapPath("../" + playFile), Server.MapPath("../" + imgFile));
    //                // invoke the helper insertVideoInfo to save the uploaded video info to the database
    //                //insertVideoInfo(playFile, imgFile);
    //            }
    //            else
    //            {
    //                //invoke the helper method changeVideoType to transform video file format
    //                if (changeVideoType(upFileName, Server.MapPath("../") + playFile, Server.MapPath("../") + imgFile))
    //                {
    //                    //invoke the custom method insertVideoInfo to save the uploaded video info to the database
    //                    // insertVideoInfo(playFile, imgFile);
    //                    // Delete uploaded videos
    //                    File.Delete(upFilePath);
    //                }
    //                else
    //                {
    //                    //RegisterStartupScript("false", "<script>alert('Upload fails!')</script>");
    //                    // Delete uploaded videos
    //                    File.Delete(upFilePath);
    //                }
    //            }
    //        }
    //        catch (Exception ex)
    //        {
    //            Response.Write(ex.Message.ToString());
    //        }
    //        //}
    //        //else
    //        //{
    //        //   // RegisterStartupScript("false", "<script>alert('File format invalid!')</script>");
    //        //}
    //    }
    //}
    //public static bool changeVideoType(string fileName, string playFile, string imgFile)
    //{
    //    //get the path of the video transformation tool
    //    string ffmpeg = System.Web.HttpContext.Current.Server.MapPath("../") + "ffmpeg.exe";
    //    //obtain the file path to transfor
    //    string Name = System.Web.HttpContext.Current.Server.MapPath("../OriginalVideo/") + fileName;//("../") +" OriginalVideo" + "/" + fileName;
    //    //if ((!System.IO.File.Exists(ffmpeg)) || (!System.IO.File.Exists(Name)))
    //    //{
    //    //    return false;
    //    //}
    //    //get the target path where to store the transformed file
    //    string flv_file = playFile;
    //    //create the instance of a process
    //    Process pss = new Process();
    //    //do not display the window
    //    pss.StartInfo.CreateNoWindow = false;
    //    // set up the path of the tool
    //    pss.StartInfo.FileName = ffmpeg;
    //    //specify the required arguments
    //    pss.StartInfo.Arguments = " -i " + Name + " -ab 128 -ar 22050 -qscale 6 -r 29.97 -s " + "widthOfFile" + "x" + "heightOfFile" + " " + flv_file;
    //    try
    //    {
    //        //start up the transform tool
    //        pss.Start();
    //        while (!pss.HasExited)
    //        {
    //            continue;
    //        }
    //        //capture the video related picture (see below)
    //        //catchImg(Name, imgFile);
    //        //System.Threading.Thread.Sleep(4000);
    //        //if (!File.Exists(imgFile))
    //        //{
    //        //File.Copy(System.Web.HttpContext.Current.Server.MapPath("../") + "imgHead\\default.gif", imgFile);
    //        //}
    //        return true;
    //    }
    //    catch
    //    {
    //        return false;
    //    }
    //}
}
